<?php

namespace App\Http\Controllers\Backend;

use App\DataTables\WechatUserDataTable;
use App\Http\Requests;
use App\Http\Requests\CreateWechatUserRequest;
use App\Http\Requests\UpdateWechatUserRequest;
use App\Repositories\WechatUserRepository;
use Flash;
use App\Http\Controllers\AppBaseController;
use Response;

class WechatUserController extends AppBaseController
{
    /** @var  WechatUserRepository */
    private $wechatUserRepository;

    public function __construct(WechatUserRepository $wechatUserRepo)
    {
        $this->wechatUserRepository = $wechatUserRepo;
    }

    /**
     * Display a listing of the WechatUser.
     *
     * @param WechatUserDataTable $wechatUserDataTable
     * @return Response
     */
    public function index(WechatUserDataTable $wechatUserDataTable)
    {
        return $wechatUserDataTable->render('backend.wechat_users.index');
    }

    /**
     * Show the form for creating a new WechatUser.
     *
     * @return Response
     */
    public function create()
    {
        return view('backend.wechat_users.create');
    }

    /**
     * Store a newly created WechatUser in storage.
     *
     * @param CreateWechatUserRequest $request
     *
     * @return Response
     */
    public function store(CreateWechatUserRequest $request)
    {
        $input = $request->all();

        $wechatUser = $this->wechatUserRepository->create($input);

        Flash::success(trans('flash.controller.save_success', ['model' => 'Wechat User']));

        return redirect(route('wechatUsers.index'));
    }

    /**
     * Display the specified WechatUser.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $wechatUser = $this->wechatUserRepository->findWithoutFail($id);

        if (empty($wechatUser)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Wechat User']));

            return redirect(route('wechatUsers.index'));
        }

        return view('backend.wechat_users.show')->with('wechatUser', $wechatUser);
    }

    /**
     * Show the form for editing the specified WechatUser.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $wechatUser = $this->wechatUserRepository->findWithoutFail($id);

        if (empty($wechatUser)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Wechat User']));

            return redirect(route('wechatUsers.index'));
        }

        return view('backend.wechat_users.edit')->with('wechatUser', $wechatUser);
    }

    /**
     * Update the specified WechatUser in storage.
     *
     * @param  int              $id
     * @param UpdateWechatUserRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateWechatUserRequest $request)
    {
        $wechatUser = $this->wechatUserRepository->findWithoutFail($id);

        if (empty($wechatUser)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Wechat User']));

            return redirect(route('wechatUsers.index'));
        }

        $wechatUser = $this->wechatUserRepository->update($request->all(), $id);

        Flash::success(trans('flash.controller.update_success', ['model' => 'Wechat User']));

        return redirect(route('wechatUsers.index'));
    }

    /**
     * Remove the specified WechatUser from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $wechatUser = $this->wechatUserRepository->findWithoutFail($id);

        if (empty($wechatUser)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Wechat User']));

            return redirect(route('wechatUsers.index'));
        }

        $this->wechatUserRepository->delete($id);

        Flash::success(trans('flash.controller.delete_success', ['model' => 'Wechat User']));

        return redirect(route('wechatUsers.index'));
    }
}
